package com.fuze.navi.controller;

import com.fuze.navi.aspect.anno.AuthAdmin;
import com.fuze.navi.common.Result;
import com.fuze.navi.domain.dto.CoordinateDTO;
import com.fuze.navi.domain.dto.InsertFormDTO;
import com.fuze.navi.domain.model.Coordinate;
import com.fuze.navi.service.NavigationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.validation.constraints.NotEmpty;

/**
 * @author sepc_
 * @date 3/16/2021 15:17
 * 管理模块相关接口
 */
@Slf4j
@RequiredArgsConstructor
@RestController
public class AdminController {


    final RestTemplate restTemplate;

    final
    NavigationService navigationService;

    /**
     * 删除指定坐标
     */
    @DeleteMapping("/admin/delete/{coordinateIds}")
    public Result deleteByCoordinateId(@PathVariable @Validated @NotEmpty(message = "要删除的ID不能为空") Long[] coordinateIds) {
        navigationService.deleteByCoordinateId(coordinateIds);
        return Result.ok();
    }

    /**
     * 更新坐标数据
     */
    @PutMapping("/admin/update")
    public Result update(@Validated CoordinateDTO coordinateDTO) {
        Coordinate coordinate = new Coordinate();
        BeanUtils.copyProperties(coordinateDTO, coordinate);
        navigationService.update(coordinate);
        return Result.ok();
    }

    /**
     * 插入表单数据
     *
     * @param insertFormDTO 表单数据
     * @return 统一返回
     */
    @AuthAdmin
    @PostMapping("/admin/insertCoordinate")
    public Result insertCoordinate(@RequestBody @Validated InsertFormDTO insertFormDTO) {
        navigationService.insert(insertFormDTO);
        return Result.ok();
    }

}
